<template>
    <!-- 车辆导入 -->
    <el-dialog :title="$t('vehicle.attachment.import.moduleName')" :visible.sync="upload.open" width="400px" append-to-body>

      <el-form label-width="108px" class="updateForm" ref="uploadForm" :model="uploadForm" :rules="uploadRules">
        <el-form-item :label="$t('vehicle.attachment.import.vehicleType')" prop="vehicleTypeId">
          <el-select v-model="uploadForm.vehicleTypeId" clearable size="small" style="width: 240px" @change="vehicleTypeChange">
            <el-option v-for="dict in vehicleTypeOptions"
                       :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
      </el-form>

      <el-button type="primary" @click="importTemplate()" style="margin-bottom: 10px">
          {{$t('vehicle.attachment.import.downloadTemplate')}}
        </el-button>

        <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url"
            :disabled="upload.isUploading" :on-success="handleFileSuccess" :on-progress="handleFileUploadProgress"
            :auto-upload="false" drag :data="uploadForm">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              {{$t('vehicle.attachment.import.uploadText')}}
              <em>{{$t('vehicle.attachment.import.clickUpload')}}</em>
            </div>
            <div class="el-upload__tip" style="color: red" slot="tip">
              {{$t('vehicle.attachment.import.uploadTip')}}
            </div>
        </el-upload>

        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="uploadSubmit" :loading="upload.isUploading">{{$t('common.button.confirm')}}</el-button>
          <el-button @click="upload.open = false">{{$t('common.button.cancel')}}</el-button>
        </div>
    </el-dialog>

</template>

<script>
import { getToken } from "@/utils/auth";
import { vehicleTypeList } from "@/api/vehicle/attachment/data";

export default {
    name: "attachImport",
    data() {
        return {
          vehicleTypeOptions: [],
          imgsList: [],
            typesList: [],
            seriesList: [],
            brandList: [],
            upload: {
                open: false,
                isUploading: false,
                headers: { Authorization: "Bearer " + getToken() },
                url: process.env.VUE_APP_BASE_API + "/vehicle/attachment/importData",
            },
            uploadForm: {
                vehicleTypeId: "",
            },
            uploadRules: {
                vehicleTypeId: [
                    {required: true, message: this.$t('vehicle.attachment.import.uploadRules.vehicleType'), trigger: "change",},
                ],
                file: "",
            },
        }
    },
    methods: {
        init() {
            this.upload.open = true;
            this.vehicleTypeList();
        },
        vehicleTypeList() {
          vehicleTypeList().then(response => {
            this.vehicleTypeOptions = response.data.map(item => {
              return {
                label: item.typeName,
                value: item.id
              }
            })
          })
        },
        vehicleTypeChange(typeId) {
          if (typeId) {
            this.getVehicleTypTree(typeId)
          } else {
            this.uploadForm.vehicleTypeId = undefined
            this.vehicleTypeOptions = []
          }
        },
        /** 下载模板操作 */
        importTemplate() {
            this.download('vehicle/attachment/importTemplate', {
            }, `vehicle_attachment_template_${new Date().getTime()}.xlsx`)
        },
        uploadSubmit() {
          this.$refs.uploadForm.validate((valid) => {
            if (valid) {
              this.$refs.upload.submit();
            }
          });
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false;
            this.upload.isUploading = false;
            this.resetForm("uploadForm");
            this.$refs.upload.clearFiles();
            this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", this.$t('common.alert.importResult'), { dangerouslyUseHTMLString: true });
            this.refreshData();
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit();
        },
        refreshData() {
            this.$emit("refreshData")
        },
        getVehicleTypTree(typeId) {

        }
    },
}
</script>

<style scoped></style>
